//
//  UNBarButtonItem.swift
//  Unibet
//
//  Created by HaoSheng on 2020/4/15.
//  Copyright © 2020 蜗牛. All rights reserved.
//

import UIKit

@MainActor
public class UNBarButtonItem: NSObject {
    
    
    ///返回只带图片的返回按钮
    static public func backitemWithImage(image: String, target: Any?, action: Selector ) -> UIBarButtonItem{
        
        let view = UIView.init(frame: .init(x: 0, y: 0, width: 40, height: 44));
        
        
        let gobackBtn = UIButton.init(frame: view.bounds)
        gobackBtn.addTarget(target, action: action, for: .touchDown);
        view.addSubview(gobackBtn);
        
        
        
        let imageView = UIImageView.init(frame: .init(x: 0, y: (44-16)/2, width: 9, height: 16));
        imageView.image = UIImage.init(named: image);
        view.addSubview(imageView);
        
        
        let barItem = UIBarButtonItem.init(customView: view);
        
        return barItem;
    }
        
    
    
    
    ///返回只带图片的按钮
    static public func itemWithImage(image: String, imageAlignment: NSTextAlignment , target: Any?, action: Selector ) -> UIBarButtonItem{
        
        let view = UIView.init(frame: .init(x: 0, y: 0, width: 40, height: 44));
        
        let btn = UIButton.init(frame: view.bounds)
        
        btn.setImage(UIImage.init(named: image), for: .normal);

        btn .setImage(UIImage.init(named: image), for: .highlighted);
        
        btn.addTarget(target, action: action, for: .touchUpInside);
        
        view.addSubview(btn)
        
        
        let w = (btn.frame.size.width - btn.imageView!.frame.size.width)/2
        let h = (btn.frame.size.height - btn.imageView!.frame.size.height)/2
        
        switch imageAlignment {
        case .left:
            btn.imageEdgeInsets = .init(top: h, left: -w, bottom: h, right: w)
            break
        case .right:
            btn.imageEdgeInsets = .init(top: h, left: w, bottom: h, right: -w)
            break
        default:
            
            break
        }
        
        
        let itemButton = UIBarButtonItem.init(customView: view);
        
        return itemButton;
    }
    
    
    
    ///返回只带文字的按钮
    static public func itemWithTitle(title: String, titleFont: UIFont,titleColor: UIColor, target: Any?, action: Selector) ->UIBarButtonItem{
        
        let btn = UIButton.init(type: .custom);
        
        btn.setTitle(title, for: .normal);
        
        btn.titleLabel?.font = titleFont;
        
        btn.setTitleColor(titleColor, for: .normal);
        
        btn.setTitleColor(titleColor, for: .highlighted);
        
        btn.sizeToFit();
        
        btn.addTarget(target, action: action, for: .touchUpInside);
        
        btn.changeButtonType(into: .leftTitleRightImage)
        
        let itemButton = UIBarButtonItem.init(customView: btn);
        
        return itemButton;
    }
    
    
    ///返回带文字和图片的按钮
    static public func itemWithTitleAndImage(title: String, titleFont: UIFont, titleColor: UIColor, imageString: String, leftTitleRightImage: Bool?, target: Any?, action: Selector) ->UIBarButtonItem{
        
        
        let btn = UIButton.init(type: .custom);
        
        btn.setTitle(title, for: .normal);
        
        btn.titleLabel?.font = titleFont;
        
        btn.setTitleColor(titleColor, for: .normal);
        
        btn.setTitleColor(titleColor, for: .highlighted);
        
        btn.setImage(UIImage.init(named: imageString), for: .normal)
        
        btn.setImage(UIImage.init(named: imageString), for: .highlighted)
        
        btn.sizeToFit();
        
        btn.addTarget(target, action: action, for: .touchUpInside);
        
        
        if leftTitleRightImage ?? false {
            
            btn.changeButtonType(into: .leftTitleRightImage)
            
        }
        
        
        
        let itemButton = UIBarButtonItem.init(customView: btn);
        
        return itemButton;
        
    }
    
    
    
    
    
}
